<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'
import { totalFlow } from "@/api/site/isRecord";
const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '250px'
        },
        chartData: {
            type: Array,
            // required: true
        },
        isNumber: {
            type: String,
        }
    },
    data() {
        return {
            chart: null,
            chartList: []
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                // this.setOptions(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    created() {
        this.totalFlowFun();
    },
    methods: {
        totalFlowFun() {
            totalFlow({
                serialNumber: this.isNumber,
                modelName: '累计流量',
            }).then(res => {
                console.log(res)
                this.chartList = res.data
                this.initChart()
            })
        },
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // this.setOptions(this.chartData)
            this.setOptions()
        },
        setOptions(numData) {
            let yData = [0, 0, 0, 0, 0, 0]
            let MAX = [yData[0], yData[0], yData[0], yData[0], yData[0], yData[0]]
            let MAXX = [110, 120, 100, 210, 30, 58]
            this.chart.setOption({
                // backgroundColor: '#000a3f',
                tooltip: {

                },
                grid: {
                    top: '10%',
                    left: '5%',
                    bottom: '5%',
                    right: '5%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: this.chartList.map(res => res.weekday),
                    axisTick: {
                        alignWithLabel: true
                    },
                    axisLabel: {
                        show: true,
                        fontSize: 11,
                    }
                }],
                yAxis: [{
                    type: 'value',
                    axisTick: {
                        show: false
                    }
                }],
                series: [
                    // 背景柱

                    {
                        name: '累计流量',
                        type: 'bar',
                        data: this.chartList.map(res => res.log_value),
                        // data: [11, 22, 33, 44, 55, 66, 77],
                        itemStyle: {
                            color: {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [
                                    { offset: 0, color: 'rgba(0, 234, 255, 1)' }, // 0% 处的颜色
                                    { offset: 1, color: 'rgba(0, 234, 255, 0.06)' } // 100% 处的颜色
                                ],
                                global: false // 缺省为 false
                            },
                        }
                    },

                ]

            })
        }
    }
}
</script>
